package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.Item;
import ysq.xyg.model.ItemCat;
import ysq.xyg.model.info.ItemInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.ItemCatService;
import ysq.xyg.service.ItemService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;

/**
 * 商品分类
 *
 * @author congcong tao
 * @date 2018/8/7 11:49
 */
@Controller
@RequestMapping("/item")
public class ItemController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(ItemController.class);
    @Resource
    private ItemService itemService;
    @Resource
    private ItemCatService itemCatService;

    private Item item;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response, String catId){
        // item,1查询分类list2已有itemId
        ModelAndView view = new ModelAndView("/itemList");
        List<ItemCat> itemCatList = itemCatService.selectAll();
        view.addObject("itemCatList", itemCatList);
        view.addObject("catId", catId);
        return view;
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject jsonList(Item item, Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<Item> pageInfo = itemService.pageInfo(item, page.getLimit(), page.getPage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", pageInfo.getTotal());
        if (pageInfo != null && pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for (Item itemInfo : pageInfo.getList()) {
                row = new JSONObject();
                row.put("id", itemInfo.getId());
                row.put("title", itemInfo.getTitle());
                row.put("catId", itemInfo.getCatId());
                row.put("catName", itemInfo.getCatName());
                row.put("type", itemInfo.getType());
                row.put("catName", itemInfo.getCatName());
                row.put("personNum", itemInfo.getPersonNum());
                row.put("minPurchaseNum", itemInfo.getMinPurchaseNum());
                row.put("price", itemInfo.getPrice());
                row.put("createTime", DateUtil.DateToStr(itemInfo.getCreateTime(), DateUtil.DATE_TIME_PATTERN));
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView selectEntity(String id, String catId){
        ModelAndView view = new ModelAndView("/itemUpdateOrSave");
        item = itemService.selectByPrimaryKey(id);
        ItemCat itemCat = itemCatService.selectByPrimaryKey(catId);
        List<ItemCat> itemCatList = itemCatService.selectAll();
        view.addObject("itemCatList", itemCatList);
        view.addObject("item", item);
        view.addObject("itemCat", itemCat);
        return view;
    }

    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateOrAddEntity(ItemInfo itemInfo) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isEmpty(itemInfo.getId())) {
            item = new Item();
            item.setId(BaseUtil.uId());
            item.setCatId(itemInfo.getCatId());
            item.setTitle(itemInfo.getTitle());
            item.setPrice(itemInfo.getPrice());
            item.setTransferPrice(itemInfo.getTransferPrice());
            item.setMinPurchaseNum(itemInfo.getMinPurchaseNum());
            item.setType(itemInfo.getType());
            item.setRedeemCode(itemInfo.getRedeemCode());
            item.setPersonNum(itemInfo.getPersonNum());
            item.setImageOne(itemInfo.getImageOne());
            item.setImageTwo(itemInfo.getImageTwo());
            item.setImageThree(itemInfo.getImageThree());
            item.setItemDesc(itemInfo.getItemDesc());
            item.setCreateTime(new Date());
            int id = itemService.insert(item);
            if(id <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            item = itemService.selectByPrimaryKey(itemInfo.getId());
            item.setCatId(itemInfo.getCatId());
            item.setTitle(itemInfo.getTitle());
            item.setPrice(itemInfo.getPrice());
            item.setTransferPrice(itemInfo.getTransferPrice());
            item.setMinPurchaseNum(itemInfo.getMinPurchaseNum());
            item.setType(itemInfo.getType());
            item.setRedeemCode(itemInfo.getRedeemCode());
            item.setPersonNum(itemInfo.getPersonNum());
            item.setImageOne(itemInfo.getImageOne());
            item.setImageTwo(itemInfo.getImageTwo());
            item.setImageThree(itemInfo.getImageThree());
            item.setItemDesc(itemInfo.getItemDesc());
            int id = itemService.updateByPrimaryKey(item);
            if (id <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }

    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject deleteEntity(String id){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
            int ids = itemService.deleteByPrimaryKey(id);
            if(ids <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
}
